// mircoApp/package_deliver_write_off/package_deliver_write_off_index/package_deliver_write_off_index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeName: 0,
    num: "",
    show: false,
    keyword: "",
    radio: null,
    list: [],
    record_list: [],
    codeImg: "",
    codeShow: false,
    coupon: {
      page: 1,
      total_page: 0,
      isLoadMore: true
    },
    isLoadMore: true,
    page: 1,
    total_page: 0,
    is_record: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.Record) {
      this.setData({
        is_record: true
      })
    }
    this.init();
    this.getRecord();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  showClose() {
    this.setData({
      show: false
    })
  },
  searchTap() {
    this.setData({
      radio: null,
      list: [],
      'coupon.page': 1,
      'coupon.total_page': 0,
      'coupon.isLoadMore': true,
    })
    this.getCoupon();
  },
  async showPop() {
    this.setData({
      radio: null,
      list: [],
      'coupon.page': 1,
      'coupon.total_page': 0,
      'coupon.isLoadMore': true,
      show: true
    })
    await this.getCoupon();
  },
  radioSelect(e) {
    let {
      item
    } = e.currentTarget.dataset;
    if (this.data.radio == item.id) {
      this.setData({
        radio: null
      })
      return;
    }
    this.setData({
      radio: item.id
    })
  },
  onClickRight(e) {
    console.log(e)
    this.setData({
      show: false
    })
  },
  init() {
    this.setData({
      list: [],
      'coupon.page': 1,
      'coupon.total_page': 0,
      'coupon.isLoadMore': true,
      page: 1,
      total_page: 0,
      isLoadMore: true
    })
  },
  async confirmM() {
    if (!this.data.radio) {
      wx.showModal({
        title: '提示',
        content: '请选择酒券',
        showCancel: false
      })
      return
    }
    if (!this.data.num) {
      wx.showModal({
        title: '提示',
        content: '请输入核销数量',
        showCancel: false
      })
      return
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.gradevin-stock.frontend.controllers.write-off.write-off-code"),
      data: {
        coupon_id: this.data.radio,
        num: this.data.num
      }
    })
    if (data.result) {
      this.setData({
        codeImg: data.data.code,
        codeShow: true
      })
    } else {
      app.tips(data.msg)
    }
  },
  onClose() {
    this.setData({
      codeShow: false
    })
  },
  async getCoupon() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.gradevin-stock.frontend.controllers.write-off.get-coupon-list"),
      data: {
        page: this.data.coupon.page,
        keyword: this.data.keyword
      }
    })
    if (data.result == 1) {
      this.setData({
        list: data.data.data,
        'coupon.total_page': data.data.last_page,
        'coupon.isLoadMore': true
      })
    } else {
      app.tips(data.msg)
    }
  },
  async getRecord() {
    let urlstr = app.getNetAddresss("plugin.gradevin-stock.frontend.controllers.write-off.write-off-log");
    if (this.data.is_record) {
      urlstr = app.getNetAddresss("plugin.gradevin-stock.frontend.controllers.write-off.clerk-write-off-log");
    }
    let {
      data
    } = await app._postNetWork({
      url: urlstr
    })

    if (data.result) {
      this.setData({
        record_list: data.data.list.data,
        total_page: data.data.list.total_page,
        isLoadMore: true
      })
    } else {
      app.tips(data.msg)
    }
  },
  async getMoreData() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false;
    if (this.data.page >= this.data.total_page) {
      return
    } else {
      let urlstr = app.getNetAddresss("plugin.gradevin-stock.frontend.controllers.write-off.write-off-log");
      if (this.data.is_record) {
        urlstr = app.getNetAddresss("plugin.gradevin-stock.frontend.controllers.write-off.clerk-write-off-log");
      }
      this.setData({
        page: this.data.page + 1
      })
      let {
        data
      } = await app._postNetWork({
        url: urlstr,
        data: {
          page: this.data.page
        }
      })
      if (data.result) {
        this.setData({
          record_list: this.data.record_list.concat(data.data.list.data)
        })
      } else {
        app.tips(data.msg);
        this.setData({
          page: this.data.page - 1
        })
      }
    }
  },
  async loadMoreData() {
    if (!this.data.coupon.isLoadMore) return;
    this.setData({
      'coupon.isLoadMore': false,
      'coupon.page': this.data.coupon.page + 1
    })
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.gradevin-stock.frontend.controllers.write-off.get-coupon-list"),
      data: {
        page: this.data.coupon.page,
        keyword: this.data.keyword
      }
    })
    if (data.result) {
      this.setData({
        list: this.data.list.concat(data.data.data)
      })
    } else {
      this.setData({
        'coupon.page': this.data.coupon.page - 1
      })
      app.tips(data.msg)
    }
  }
})